<template>
  <div>
    <br />
    <p style="text-align:center "> <a-icon style="color:red;font-size:15px" @click="gwc" type="plus" />购物车</p>
    <br />
    <div style="width:93%;margin:0 auto;;">
      <div
        class="div_top_yuan"
        style="width:40px;height:40px"
        v-for="(item, index) in size"
        :key="index"
      >
        <span @click="onclick(item,index)">{{item}}</span>
      </div>
    </div>

    <div style="width:20%;margin:0 auto;height:10vh;line-height:10vh">
      <span @click="toplist">升序</span>&nbsp;
      <span @click="footlist">降序</span>
    </div>
    <br />
    <br />
    <div v-for="(item, index) in list" :key="index" @click="cart(item)">
      <a-card hoverable style="width:43%;float:left;margin-left:4.7%;margin-top:4%">
        <img slot="cover" alt="example" :src="item.mainPic" />
        <a-card-meta :title="item.title">
          <template slot="description">{{item.shipPercent}}</template>
          <template slot="description">
            <a-icon style="color:red;font-size:15px" type="plus" />
          </template>
        </a-card-meta>
      </a-card>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      cartlist: [],
      cartyes: [],
      list: [],
      size: ["全部", "xs", "s", "m", "ml", "l", "xl", "xxl"],
    };
  },

  created() {
    let cartyes = localStorage.cartyes;
    if (cartyes) {
      this.cartyes = JSON.parse(cartyes);
    }
  },
  methods: {
      gwc(){
    this.$router.push('about')
  },
    onclick(item, index) {
      if (index === 0) {
        this.list = [];
        this.cartlist.forEach((ele) => {
          this.list.push(ele);
        });
      } else {
        this.cartlist.forEach((ele) => {
          if (item === ele.size) {
            this.list = [];
            this.list.push(ele);
          }
        });
      }
    },
    cart(item) {
      console.log(item);
      var zt = false;
      this.cartyes.forEach((ele) => {
        if (ele.id === item.id) {
          zt = true;
          item.num++;
        }
      });
      if (!zt) {
        item.num = 1;
        this.cartyes.push(item);
      }
      console.log( this.cartyes)

      localStorage.cartyes = JSON.stringify(this.cartyes);
    },
    toplist() {
      this.cartlist.sort((a, b) => {
        return a.shipPercent - b.shipPercent;
      });
    },
    footlist() {
      this.cartlist.sort((a, b) => {
        return b.shipPercent - a.shipPercent;
      });
    },
  },
  mounted() {
    axios.get("http://localhost:8080/goodsList.json").then((res) => {
      console.log(res.data.result.data.list);
      this.cartlist = res.data.result.data.list;
      this.list = res.data.result.data.list;
    });
  },
};
</script>

<style  scoped>
.div_top_yuan {
  border-radius: 50%;
  line-height: 40px;
  text-align: center;
  float: left;
  margin-left: 1%;
  background: rgb(223, 223, 223);
}
</style>

